package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ProxyActionRequest类
 * 实现代理动作请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxyActionRequest implements AxdrType {

    private byte[] dataCode = null;
    private TSA tsa = null;
    private Unsigned16 maxWait = null;
    private SubSeqOfProxyOmdReads reads = null;

    public ProxyActionRequest() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(TSA tsa, Unsigned16 maxWait, SubSeqOfProxyOmdReads reads) {
        this.tsa = tsa;
        this.maxWait = maxWait;
        this.reads = reads;
    }

    public void setTsa(TSA tsa) {
        this.tsa = tsa;
    }

    public void setMaxWait(Unsigned16 maxWait) {
        this.maxWait = maxWait;
    }

    public void setReads(SubSeqOfProxyOmdReads reads) {
        this.reads = reads;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);

        this.maxWait = new Unsigned16();
        this.maxWait.setConst();
        codeLength += this.maxWait.decode(input);

        this.reads = new SubSeqOfProxyOmdReads();
        codeLength += this.reads.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.tsa != null && this.maxWait != null && this.reads != null) {
                codeLength = 0;
                codeLength += this.reads.encode(output);
                codeLength += this.maxWait.encode(output);
                codeLength += this.tsa.encode(output);
            } else {
                throw new RuntimeException("reads || maxWait || tsa is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding ProxyActionRequest", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.reads != null && this.maxWait != null && this.tsa != null) {
            return "sequence: {" + "tsa: " + this.tsa + ", maxwait: " + this.maxWait + ", reads: " + this.reads + "}";
        } else {
            return "reads || maxWait || tsa is null";
        }
    }

    public TSA getTsa() {
        return tsa;
    }

    public Unsigned16 getMaxWait() {
        return maxWait;
    }

    public SubSeqOfProxyOmdReads getReads() {
        return reads;
    }
}